package com.hyg.lib_base.base;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;

import com.hyg.lib_base.Net.BrokenNetReceiver;
import com.kingsoft.media.httpcache.KSYProxyService;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;

import androidx.multidex.BuildConfig;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

public class MyBaseApplication extends MultiDexApplication {

    public static MyBaseApplication instance;
    public BrokenNetReceiver brokenNetReceiver;
    public IntentFilter filter;
    private static final String TAG = "BaseApplication";
    private static Context context;





    public MyBaseApplication() {
    }

    public static MyBaseApplication getInstance() {
        return instance;
    }

    public static Context getAppContext() {
        return context;
    }


    @SuppressLint("NewApi")
    @Override
    public void onCreate() {


        super.onCreate();
        context = getApplicationContext();
        instance = this;
        /**
         * 1、首先创建了一个IntentFilter实例,并给它添加了一个值为android.net.conn.CONNECTIVITY_CHANGE的action
         * 2、当网络发生变化时,系统发出的就是"android.net.conn.CONNECTIVITY_CHANGE"的广播
         * 3、即我们的广播接收器里面想要监听什么广播,就在这里添加相应的action
         * 4、接着创建一个NetworkChangeReceiver的实例,然后调用registerReceiver方法进行注册,将networkChangeReceiver、networkChangeReceiver都传入
         * 5、这样NetworkChangeReceiver就会收到值为"android.net.conn.CONNECTIVITY_CHANGE"的广播
         * 6、重要:动态注册的广播接收器一定要取消注册才行,调用unregisterReceiver()方法进行取消
         */
        filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //在网络连接中出现的一个变化。已被建立或断开的一个连接。
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        filter.addAction("android.Net.wifi.WIFI_STATE_CHANGED");
        filter.addAction("android.net.wifi.STATE_CHANGE");
        brokenNetReceiver = new BrokenNetReceiver();
        registerReceiver(brokenNetReceiver, filter);

        //        if (LeakCanary.isInAnalyzerProcess(this)) {
        //            return;
        //        }
        //        LeakCanary.install(this);
        //路由
        initRouter(instance);
        //友盟init
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_BOX, null);
        UMConfigure.setLogEnabled(true);
        // 加载子模块的 自定义Application
        modulesApplicationInit();
        CrashReport.initCrashReport(getApplicationContext(), "2696263925", true);
    }

    public static void initRouter(Application application) {
        if (BuildConfig.DEBUG) {
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(application);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    //通过反射方法找到对应的类，并调用接口
    private void modulesApplicationInit() {
        for (String moduleImpl : ModelConfig.MODULESLIST) {
            try {
                Class<?> clazz = Class.forName(moduleImpl);
                Object object = clazz.newInstance();
                Log.d(TAG, "clazz.newInstance");

                if (object instanceof ApplicationImpl) {
                    ((ApplicationImpl) object).onCreate(this);
                    Log.d(TAG, "Class.onCreate");
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onTerminate() {
        // 程序终止的时候执行
        Log.d(TAG, "onTerminate");
        super.onTerminate();
        unregisterReceiver(brokenNetReceiver);
    }

    @Override
    public void onLowMemory() {
        // 低内存的时候执行
        Log.d(TAG, "onLowMemory");
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        // 程序在内存清理的时候执行
        Log.d(TAG, "onTrimMemory");
        super.onTrimMemory(level);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.d(TAG, "onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }


}